/**
 * Google Chart Functions
 */
google.load("visualization", "1", {	packages : ["corechart"] });

$(function(){
	$('#poll-result-question').live('pageshow', function(){
		if(google.visualization){
			$.mobile.showPageLoadingMsg();
			Chart.draw();
		}else{
			google.setOnLoadCallback(Chart.draw);
		}
	});
})

var Chart = {
	loaded : false,
	
	draw: function(){
		Chart.drawQuestion();
		if($('#chart_gender_div').length > 0)
		{
			Chart.drawGender();
			Chart.drawAge();
			Chart.drawEducation();
		}
		$.mobile.hidePageLoadingMsg();
	},
	
	drawQuestion : function() {
		var pageId = $.mobile.activePage.attr('id');
		if(pageId.substr(0, 11) == 'poll-result'){
			for(var i in RESULT_DATA) {
				var data = new google.visualization.DataTable();
				var rawRes = RESULT_DATA[i];
				var convertedRes = [];
				for(var j in rawRes) {
					convertedRes.push([j, rawRes[j]]);
				}
		
				data.addColumn('string', '성');
				data.addColumn('number', '(명)');
				data.addRows(convertedRes);
		
				var $container = $(".chart-div").eq(i);
		
				var options = {
					width : $container.prop('width'),
					height : 240,
					vAxis : {
						viewWindow : {
							min : 0
						},
						gridlines : {
							count : 2
						}
					},
					backgroundColor: 'transparent'
				};
				var chart = new google.visualization.ColumnChart($container.get(0));
				chart.draw(data, options);
			}
		}
	},
	
	drawGender: function() {
		var data = new google.visualization.DataTable();
		data.addColumn('string', '성');
		data.addColumn('number', '(명)');
		data.addRows([['남성', RESULT_VOTE.male], ['여성', RESULT_VOTE.female]]);

		var $container = $('#chart_gender_div');
		var options = {
			width : $container.prop('width'),
			height : 240,
			title : '성별 분포 비율',
			hAxis : {
				title : '성별'
			},
			vAxis : {
				viewWindow : {
					min : 0
				},
				gridlines : {
					count : 2
				}
			},
			backgroundColor: 'transparent'
		};

		var chart = new google.visualization.ColumnChart($container.get(0));
		chart.draw(data, options);
	}, 
	
	drawAge: function() {
		var data = new google.visualization.DataTable();
		data.addColumn('string', '연령대');
		data.addColumn('number', '(명)');
		
		var $container = $('#chart_age_div');
		
		var rows = [];
		for(var i = 0; i<9; i++){
			rows.push( [(i+1)+'0대'+(i==8?'이상':''), RESULT_VOTE.years[i] ]);
		}
		data.addRows(rows);
		var options = {
			width : $container.prop('width'),
			height : 240,
			title : '연령대별 분포 비율',
			hAxis : {
				title : '연령대',
				maxValue : 11
			},
			vAxis : {
				viewWindow : {
					min : 0
				},
				gridlines : {
					count : 2
				}
			},
			backgroundColor: 'transparent'
		};

		var chart = new google.visualization.ColumnChart($container.get(0));
		chart.draw(data, options);
	}, drawEducation : function() {
		var data = new google.visualization.DataTable();
		data.addColumn('string', '학력');
		data.addColumn('number', '(명)');
		data.addRows([['중졸이하', RESULT_VOTE.education[0]], ['고졸', RESULT_VOTE.education[1]], ['대졸', RESULT_VOTE.education[2]], ['초대졸', RESULT_VOTE.education[3]], ['대학원졸', RESULT_VOTE.education[4]]]);

		var $container = $('#chart_education_div');
		console.log($container.parent('li'))
		var options = {
			width : $container.prop('width'),
			height : 240,
			title : '학력별 분포 비율',
			hAxis : {
				title : '학력'
			},
			vAxis : {
				viewWindow : {
					min : 0
				},
				gridlines : {
					count : 2
				}
			},
			backgroundColor: 'transparent'
		};

		var chart = new google.visualization.ColumnChart($container.get(0));
		chart.draw(data, options);
	}
}
